﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text.RegularExpressions;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// “空白页”项模板在 http://go.microsoft.com/fwlink/?LinkId=234238 上有介绍

namespace LikeBangumi.Pages
{
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class FCInfoPage : Page
    {
        public delegate void PlayVideo(PlayModel model);
        public event PlayVideo playEvent;
        public FCInfoPage()
        {
            this.InitializeComponent();
            NavigationCacheMode = NavigationCacheMode.Required;
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            mo.Info = e.Parameter as string;
            mo.Type = 3;
            GetISAtt(mo.Info);
            GetList(e.Parameter as string);
            
        }
        MyBangumi mo = new MyBangumi();
        private void list_Video_ItemClick(object sender, ItemClickEventArgs e)
        {
            FCInfoModel model = e.ClickedItem as FCInfoModel;
         
            playEvent(new PlayModel() { type = BangumiType.Fengche, info = model.url,part=model.num });
            //this.Frame.Navigate(typeof(TestPlayerPage), (e.ClickedItem as DModel).url);
        }
        WebClientClass wc;
        private async void GetList(string name)
        {
            try
            {
                pr_Load.Visibility = Visibility.Visible;
                wc = new WebClientClass();

                string results = await wc.GetResults(new Uri(name));
                DModel info = new DModel();
                info.title = Regex.Match(results, @"<h1 class=""c"">(.*?)<sub>.*?</sub>", RegexOptions.Singleline).Groups[1].Value.Trim();
                info.img = Regex.Match(results, @"<div class=""pic"">.*?<img src='(.*?)'.*?/></div>", RegexOptions.Singleline).Groups[1].Value;
                info.area = Regex.Match(results, @"<p>作者：(.*?)</p>").Groups[1].Value;
                info.date = Regex.Match(results, @"<p>版本：(.*?)</p>").Groups[1].Value;
                info.tag = Regex.Match(results, @"<p>分类：<a href="".*?"">(.*?)</a></p>").Groups[1].Value;
                info.status = Regex.Match(results, @"<p>状态：(.*?)</p>").Groups[1].Value;
                info.desc = Regex.Match(results, @"<strong>剧情简介：</strong>(.*?)</p>").Groups[1].Value;
                BanSeason_Grid.DataContext = info;
                mo.Title = info.title;
                mo.Img = info.img;
               


                MatchCollection mc = Regex.Matches(results, @"<div class='blank'></div><div class='tit'><h2>(.*?)</h2></div><div class='movurl'>(.*?)</div>", RegexOptions.Singleline);

                List<FCInfoModel> listModel = new List<FCInfoModel>();
                foreach (Match item in mc)
                {
                    List<FCInfoModel> ls = new List<FCInfoModel>();
                    foreach (Match item2 in Regex.Matches(item.Groups[2].Value, "<li><a href='(.*?)' target='_blank'>(.*?)</a></li>"))
                    {
                        ls.Add(new FCInfoModel() {
                            url = "http://m.dm530.net" + item2.Groups[1].Value,
                            num = item2.Groups[2].Value,
                        });
                    }
                    listModel.Add(new FCInfoModel()
                    {
                        title = item.Groups[1].Value,
                        list= ls
                    });
                }

                if (listModel.Count!=0)
                {
                    mo.New = listModel[0].num;
                }
                items.ItemsSource = listModel;
                //list_Video.ItemsSource = listModel;
            }
            catch (Exception)
            {
                messShow.Show("读取失败", 3000);
            }
            finally
            {
                pr_Load.Visibility = Visibility.Collapsed;
            }

        }

        private void btn_AddZF_Click(object sender, RoutedEventArgs e)
        {
            using (var conn = AppDatabase.GetDbConnection())
            {
                // 受影响行数。
                var count = conn.Insert(mo);
                if (count == 1)
                {
                    messShow.Show("已经添加到我的追番", 3000);
                    btn_AddZF.IsEnabled = false;
                }
                else
                {
                    messShow.Show("添加失败", 3000);
                }
            }
        }
        private void GetISAtt(string info)
        {
            using (var conn = AppDatabase.GetDbConnection())
            {
                // 受影响行数。
                var m = from p in conn.Table<MyBangumi>()
                        where p.Info == info
                        select p;
                if (m.ToList().Count == 0)
                {
                    btn_AddZF.IsEnabled = true;
                }
                else
                {
                    btn_AddZF.IsEnabled = false;
                }
            }
        }
    }

    public class FCInfoModel
    {
       // public string title { get; set; }
       public List<FCInfoModel> list { get; set; }
        public string url { get; set; }
        public string num { get; set; }
        public string desc { get; set; }

        public string title { get; set; }
        public string img { get; set; }
        public string area { get; set; }
        public string date { get; set; }
        public string tag { get; set; }
        public string status { get; set; }
        //public string desc { }
    }

}
